var express = require('express');
var router = express.Router();
const moment =require('moment');
// var user = require('../models/user');
var Category = require('../models/category');
var Content = require('../models/content');
router.use(function (req, res, next) {
    if (!req.userInfo.isAdmin) {
        res.send('你不是管理员');
        return;
    }
    next();
});

router.get('/',function (req, res, next) {

    res.render('admin/index',{
        userInfo:req.userInfo
    });
})
/**
 * 用户管理
 */
router.get('/user',function (req, res, next) {
    res.render('admin/user_index',{
        userInfo:req.userInfo
    });
})
/**
 * 分类管理,首页
 */
router.get('/category',function (req, res, next) {
    res.render('admin/category_index',{
        userInfo:req.userInfo
    });
})
/**
 * 分类管理,添加
 */
router.get('/category/add',function (req, res, next) {//添加页面
    res.render('admin/category_add',{
        userInfo:req.userInfo
    });
})

router.post('/category/add',function (req, res, next) {//添加post请求处理
    var name = req.body.name || '';
    if (name == '') {
        res.render('admin/error',{
            userInfo:req.userInfo,
            message:'名称不能为空'
        });
        return;
    }
    //判断数据库是否存在

    Category.findOne({name: name}).then(function (info) {
        if (info) {
            res.render('admin/error',{
                userInfo:req.userInfo,
                message:'名称已经存在'
            });
            return;
        }else {
            new Category({
                name:name
            }).save(function (err, doc) {
                res.render('admin/success',{
                    userInfo:req.userInfo,
                    message:'保存成功',
                    url:'/admin/category'
                })
            })
        }
    })
})
/**
 * 删除分类请求
 *
 */
router.get('/category/delete',function (req, res) {
    var id = req.query.id;
    Category.remove({_id:id}).then(function (info) {
        res.render('admin/category_index',{
            userInfo:req.userInfo
        });
    })
})
/**
 * 编辑分类请求
 */
router.get('/category/edit',function (req, res) {
    var id = req.query.id;
    Category.findOne({_id:id}).then(function (info) {
        res.render('admin/category_edit',{
            userInfo:req.userInfo,
            info:info
        });
    })
})

router.post('/category/edit',function (req, res) {
    var name = req.body.name || '';
    var id = req.query.id;
    if (name == '') {
        res.render('admin/error',{
            userInfo:req.userInfo,
            message:'名称不能为空'
        });
        return;
    }
    Category.findOne({_id:id}).then(function (info) {
        if (!info) {
            res.render('admin/error',{
                userInfo:req.userInfo,
                message:'分类信息不存在'
            });
            return Promise.reject();
        }else {
            if (name == info.name) {
                res.render('admin/success',{
                    userInfo:req.userInfo,
                    message:'修改成功',
                    url:'/admin/category'
                })
            }else {
                Category.update({
                    _id:id
                },{
                    name:name
                }).then(function (info) {
                    res.render('admin/success',{
                        userInfo:req.userInfo,
                        message:'修改成功',
                        url:'/admin/category'
                    })
                })
            }
        }
    })
})
/**
 * 内容管理主页
 */
router.get('/content',function (req, res, next) {
    res.render('admin/content_index',{
        userInfo:req.userInfo
    });
})
/**
 * 内容添加
 */
router.get('/content/add',function (req, res, next) {//添加页面
    Category.find().then(function (category) {
        res.render('admin/content_add',{
            userInfo:req.userInfo,
            categorys:category
        });
    })
})

router.post('/content/add',function (req, res, next) {//添加post请求处理
    var title = req.body.title || '';
    var category = req.body.category || '';
    var description = req.body.description || '';
    var content = req.body.content || '';
    var username = req.userInfo.username;
    var date = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
    if (title == '') {
        res.render('admin/error',{
            userInfo:req.userInfo,
            message:'标题不能为空'
        });
        return;
    }
    //判断数据库是否存在

   Content.findOne({title: title}).then(function (info) {
        if (info) {
            res.render('admin/error',{
                userInfo:req.userInfo,
                message:'标题已经存在'
            });
            return;
        }else {
            new Content({
                category:category,
                title:title,
                description:description,
                content:content,
                username:username,
                date:date
            }).save(function (err, doc) {
                res.render('admin/success',{
                    userInfo:req.userInfo,
                    message:'保存成功',
                    url:'/admin/content'
                })
            })
        }
    })
})

/**
 * 内容删除
 */
router.get('/content/delete',function (req, res) {
    var id = req.query.id;
    Content.remove({_id:id}).then(function (info) {
        res.render('admin/content_index',{
            userInfo:req.userInfo
        });
    })
})

/**
 * 编辑内容请求
 */
router.get('/content/edit',function (req, res) { //页面渲染
    var id = req.query.id;
    Category.find().then(function (category) {
       Content.findOne({_id:id}).populate('category').then(function (info) {
            res.render('admin/content_edit',{
                userInfo:req.userInfo,
                info:info,
                categorys:category
            });
        })
    })
})
router.post('/content/edit',function (req, res) {
    var id = req.query.id;
    var title = req.body.title || '';
    var category = req.body.category || '';
    var description = req.body.description || '';
    var content = req.body.content || '';
    Content.update({
        _id:id
    },{
        title:title,
        category:category,
        description:description,
        content:content
    }).then(function (info) {
        res.render('admin/success',{
            userInfo:req.userInfo,
            message:'修改成功',
            url:'/admin/content'
        })
    })
})
module.exports = router;